package com.yellowpage.bigdata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.data.NspFatallogCommuAssDayRegionSum;
import com.yellowpage.bigdata.dao.NspFatallogCommuAssDayRegionSumDao;

public class NspFatallogCommuAssDayRegionSumServiceImpl extends BaseServiceImpl
		implements NspFatallogCommuAssDayRegionSumService {
	private NspFatallogCommuAssDayRegionSumDao nspFatallogCommuAssDayRegionSumDao;

	public void setNspFatallogCommuAssDayRegionSumDao(
			NspFatallogCommuAssDayRegionSumDao nspFatallogCommuAssDayRegionSumDao) {
		this.nspFatallogCommuAssDayRegionSumDao = nspFatallogCommuAssDayRegionSumDao;
	}

	/* Smooth generate Method */
	public NspFatallogCommuAssDayRegionSum getNspFatallogCommuAssDayRegionSum(
			Long id) {
		return nspFatallogCommuAssDayRegionSumDao.get(id);
	}

	public List<NspFatallogCommuAssDayRegionSum> getAllNspFatallogCommuAssDayRegionSum() {
		return nspFatallogCommuAssDayRegionSumDao.getAll();
	}

	public List<Map> queryNspFatallogCommuAssDayRegionSumByPage(Map queryMap,
			PageRender pageRender) {
		String t2 = queryMap.get("regionCode").equals("") ? ""
				: " and REGION_CODE in " + queryMap.get("regionCode");
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"SELECT  a.LOG_DATE , a.REGION_CODE , a.NUMN_DAY_COUNT , a.NUM_DAY_COUNT , a.NUMN_ALL_COUNT , a.NUM_ALL_COUNT ,"
								+ "(select c.name from OMC_REGION c where c.REGION_CODE=a.REGION_CODE) as REGION_NAME "
								+ " FROM NSP_FATALLOG_COMMUASS_DAYREGION_SUM a WHERE 1=1 "+ t2)
				.addCondition(" and LOG_DATE >= :logDate ")
				.addCondition(" and LOG_DATE <= :logDate_end ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	@Override
	public Map<String, String> getRegionMap(String regionSql) {
		Map<String, String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" select a.REGION_CODE as NAME, a.SHORT_NAME as VALUE from OMC_REGION a where 1 = 1 "
						+ regionSql);
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("NAME").toString(),
					resultList.get(i).get("VALUE").toString());
		}
		return dataSourceMap;
	}

	public void saveNspFatallogCommuAssDayRegionSum(
			NspFatallogCommuAssDayRegionSum nspFatallogCommuAssDayRegionSum) {
		nspFatallogCommuAssDayRegionSumDao
				.save(nspFatallogCommuAssDayRegionSum);
	}

	public void updateNspFatallogCommuAssDayRegionSum(
			NspFatallogCommuAssDayRegionSum nspFatallogCommuAssDayRegionSum) {
		nspFatallogCommuAssDayRegionSumDao
				.update(nspFatallogCommuAssDayRegionSum);
	}

	public void delete(List deleteList) {
		nspFatallogCommuAssDayRegionSumDao.remove(deleteList);
	}

}